/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package db.dao;

import db.Database;
import db.entities.Conta;
import db.entities.Mensagem;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

/**
 *
 * @author eduardo.lopes
 */
public final class MensagemDAO {
    
    public static int getRowCount() {
        return Database.getTableRowCount("messages");
    }
    
    public static Mensagem getMensagemById(String id) {
        Mensagem msg = null;
        try {
            Connection conn = Database.getInstance().getConnection();
            String sql = "select idmessage, assunto, de, dataehora, data, hora, responderpara, desinscrever, servidor, conta from messages where idmessage = ?";
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setString(1, id);
            ResultSet rs = ps.executeQuery();
            if(rs.next()) {
                msg = new Mensagem();
                msg.setId(rs.getString("idmessage"));
                msg.setAssunto(rs.getString("assunto"));
                msg.setDe(rs.getString("de"));
                msg.setDataehora(rs.getString("dataehora"));
                msg.setData(rs.getString("data"));
                msg.setServidor(rs.getString("servidor"));
                msg.setHora(rs.getString("hora"));
                msg.setResponderpara(rs.getString("responderpara"));
                msg.setDesinscrever(rs.getString("desinscrever"));
                msg.setConta(ContaDAO.getAccountById(rs.getInt("conta")));
            }
            rs.close();
            ps.close();
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
        return msg;
    }
    
    public static ArrayList<Mensagem> getAllMensagensByArraylist() {
        ArrayList<Mensagem> lista = new ArrayList<Mensagem>();
        try {
            Connection conn = Database.getInstance().getConnection();
            String sql = "select idmessage, assunto, de, dataehora, data, hora, responderpara, desinscrever, servidor, conta from messages";
            Statement stmt = conn.createStatement();
            ResultSet rs = stmt.executeQuery(sql);
            while(rs.next()) {
                Mensagem msg = new Mensagem();
                msg = new Mensagem();
                msg.setId(rs.getString("idmessage"));
                msg.setAssunto(rs.getString("assunto"));
                msg.setDe(rs.getString("de"));
                msg.setDataehora(rs.getString("dataehora"));
                msg.setData(rs.getString("data"));
                msg.setServidor(rs.getString("servidor"));
                msg.setHora(rs.getString("hora"));
                msg.setResponderpara(rs.getString("responderpara"));
                msg.setDesinscrever(rs.getString("desinscrever"));
                msg.setConta(ContaDAO.getAccountById(rs.getInt("conta")));
                lista.add(msg);
            }
            rs.close();
            stmt.close();
        } catch (SQLException ex) {
            ex.printStackTrace();
        }        
        return lista;
    }
    
}
